package com.zahb.qadx.ui.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseDelegateMultiAdapter;
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.zahb.qadx.R;
import com.zahb.qadx.model.MainFunc1;
import com.zahb.qadx.model.MainFunc2;
import com.zahb.qadx.ui.activity.VideoDetailsActivity;
import com.zahb.qadx.ui.activity.VideoListActivity;
import com.zahb.qadx.ui.view.decoration.MColorDrawable;
import com.zahb.qadx.ui.view.decoration.MDividerItemDecoration;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.ImageLoaderKt;
import com.zahb.qadx.util.Time;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by Zdw on 2020/10/12 11:36
 * 音频
 */
public class AudiosFragment extends HomeFuncFragment {

    static final String TAG = "AudiosFragment";

    private AudiosAdapter mAudiosAdapter;

    static AudiosFragment newInstance(MainFunc1 mainFunc1) {
        Bundle args = new Bundle();
        args.putSerializable("data", mainFunc1);
        AudiosFragment fragment = new AudiosFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTvTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.ic_home_audios, 0, 0, 0);
        MainFunc1 mainFunc1 = (MainFunc1) mArguments.getSerializable("data");
        if (mainFunc1 != null) {
            mTvTitle.setText(mainFunc1.getFunctionName());
            List<MainFunc2> mainFunc2s = (List<MainFunc2>) mainFunc1.getInfos();
//            mAudiosAdapter.addData(mainFunc2s.size() > 4 ? mainFunc2s.subList(0, 4) : mainFunc2s);//控制首页显示子布局的数量
            mAudiosAdapter.addData(mainFunc2s);
        }
    }

    @NonNull
    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
    }

    @Nullable
    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        MDividerItemDecoration itemDecoration = new MDividerItemDecoration(getContext()) {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                if (parent.getAdapter() == null) return;
                if (parent.getChildAdapterPosition(view) == 0) {// 第一行或第一列不进行偏移
                    outRect.set(0, 0, 0, 0);
                } else {
                    super.getItemOffsets(outRect, view, parent, state);
                }
            }
        };
        itemDecoration.setDrawable(new MColorDrawable(CompatHelper.getColor(R.color.gray_999999)).setIntrinsicHeight(1));
        itemDecoration.setShowLast(false);
        return itemDecoration;
    }

    @NonNull
    @Override
    protected RecyclerView.Adapter<BaseViewHolder> getAdapter() {
        mAudiosAdapter = new AudiosAdapter();
        mAudiosAdapter.setOnItemClickListener((adapter, view, position) -> {
            MainFunc2 mainFunc2 = mAudiosAdapter.getItem(position);
            VideoDetailsActivity.actionStart(getContext(), mainFunc2, VideoDetailsActivity.TYPE_AUDIO);
        });
        return mAudiosAdapter;
    }

    @Override
    protected void seeMore() {
        VideoListActivity.actionStartAudio(getContext());
    }

    public static class AudiosAdapter extends BaseDelegateMultiAdapter<MainFunc2, BaseViewHolder> {

        private static final int TYPE_PIC = 0;
        private static final int TYPE_TEXT = 1;

        AudiosAdapter() {
            setMultiTypeDelegate(new BaseMultiTypeDelegate<MainFunc2>() {
                @Override
                public int getItemType(@NotNull List<? extends MainFunc2> list, int i) {
                    if (i == 0)
                        return TYPE_PIC;
                    else
                        return TYPE_TEXT;
                }
            });
            assert getMultiTypeDelegate() != null;
            getMultiTypeDelegate().addItemType(TYPE_PIC, R.layout.item_list_audios_type_pic);
            getMultiTypeDelegate().addItemType(TYPE_TEXT, R.layout.item_list_audios_type_text);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder baseViewHolder, MainFunc2 mainFunc2) {
            if (baseViewHolder.getItemViewType() == TYPE_PIC) {
                ImageLoaderKt.loadImage(baseViewHolder.getView(R.id.iv_icon), mainFunc2.getIcon());
                baseViewHolder
                        .setText(R.id.tv_title, mainFunc2.getTitle())
                        .setText(R.id.tv_sub_title, CompatHelper.isEmpty(mainFunc2.getSubTitle())
                                ? "碎片化随时随地学" : mainFunc2.getSubTitle())
                        .setText(R.id.tv_count, CompatHelper.isEmpty(mainFunc2.getUserNum())
                                ? Time.setTime(2000)+"" : mainFunc2.getUserNum());
            } else if (baseViewHolder.getItemViewType() == TYPE_TEXT) {
                TextView tvTitle = baseViewHolder.getView(R.id.tv_title);
                tvTitle.setText(mainFunc2.getTitle());
                if (baseViewHolder.getAdapterPosition()==1){
                    TextView tvCount = baseViewHolder.getView(R.id.tv_count);
                    tvCount.setText(CompatHelper.isEmpty(mainFunc2.getUserNum())
                            ? Time.setTime(1500)+"" : mainFunc2.getUserNum());
                }else if(baseViewHolder.getAdapterPosition()==2){
                    TextView tvCount = baseViewHolder.getView(R.id.tv_count);
                    tvCount.setText(CompatHelper.isEmpty(mainFunc2.getUserNum())
                            ? Time.setTime(1300)+"" : mainFunc2.getUserNum());
                }else if(baseViewHolder.getAdapterPosition()==3){
                    TextView tvCount = baseViewHolder.getView(R.id.tv_count);
                    tvCount.setText(CompatHelper.isEmpty(mainFunc2.getUserNum())
                            ? Time.setTime(1100)+"" : mainFunc2.getUserNum());
                }


            }
        }
    }
}
